add params to db/seed.rb; provide better heroku login detection

Andrew Cantino 10 years ago
parent
commit
732d353517
3 changed files with 46 additions and 15 deletions
  1. 1 1
      app/models/agents/peak_detector_agent.rb
  2. 41 10
      bin/setup_heroku
  3. 4 4
      db/seeds.rb

+ 1 - 1
app/models/agents/peak_detector_agent.rb

@@ -38,7 +38,7 @@ module Agents
38 38
         'expected_receive_period_in_days' => "2",
39 39
         'group_by_path' => "filter",
40 40
         'value_path' => "count",
41
-        'message' => "A peak was found"
41
+        'message' => "A peak of {{count}} was found in {{filter}}"
42 42
       }
43 43
     end
44 44
 

+ 41 - 10
bin/setup_heroku

@@ -1,34 +1,35 @@
1 1
 #!/usr/bin/env ruby
2 2
 require 'open3'
3
+require 'io/console'
3 4
 
4 5
 unless `which heroku` =~ /heroku/
5 6
   puts "It looks like the heroku command line tool hasn't been installed yet.  Please install"
6
-  puts "the Heroku Toolback from https://toolbelt.heroku.com, run 'heroku auth:login', and then"
7
+  puts "the Heroku Toolbelt from https://toolbelt.heroku.com, run 'heroku auth:login', and then"
7 8
   puts "run this script again."
8 9
   exit 1
9 10
 end
10 11
 
11 12
 def capture(cmd, opts = {})
12 13
   o, s = Open3.capture2e(cmd, opts)
13
-  o
14
+  o.strip
14 15
 end
15 16
 
16
-def ask(question)
17
+def ask(question, opts = {})
17 18
   print question + " "
18 19
   STDOUT.flush
19
-  gets.strip
20
+  (opts[:noecho] ? STDIN.noecho(&:gets) : gets).strip
20 21
 end
21 22
 
22
-def nag(question)
23
+def nag(question, opts = {})
23 24
   answer = ''
24 25
   while answer.length == 0
25
-    answer = ask(question)
26
+    answer = ask(question, opts)
26 27
   end
27 28
   answer
28 29
 end
29 30
 
30 31
 def yes?(question)
31
-  ask(question) =~ /^y/i
32
+  ask(question + " (y/n)") =~ /^y/i
32 33
 end
33 34
 
34 35
 def grab_heroku_config
@@ -44,6 +45,11 @@ def grab_heroku_config
44 45
   config
45 46
 end
46 47
 
48
+unless File.exists?(File.expand_path("~/.netrc")) && File.read(File.expand_path("~/.netrc")) =~ /heroku/
49
+  puts "It looks like you need to log in to Heroku.  Please run 'heroku auth:login' before continuing."
50
+  exit 1
51
+end
52
+
47 53
 puts "Welcome #{`heroku auth:whoami`.strip}!  It looks like you're logged into Heroku."
48 54
 puts
49 55
 
@@ -51,7 +57,7 @@ info = capture("heroku info")
51 57
 if info =~ /No app specified/i
52 58
   puts "It looks like you don't have a Heroku app set up yet for this repo."
53 59
   puts "You can either exit now and run 'heroku create', or I can do it for you."
54
-  if yes?("Would you like me to create a heroku app for you now in this repo? (y/n)")
60
+  if yes?("Would you like me to create a Heroku app for you now in this repo?")
55 61
     puts `heroku create`
56 62
     info = capture("heroku info")
57 63
   else
@@ -72,7 +78,7 @@ if config.length > 0
72 78
   puts
73 79
   puts "Your current Heroku config:"
74 80
   config.each do |key, value|
75
-    puts '  ' + key + ' ' * (20 - key.length) + '= ' + value
81
+    puts '  ' + key + ' ' * (25 - [key.length, 25].min) + '= ' + value
76 82
   end
77 83
 end
78 84
 
@@ -105,7 +111,7 @@ unless config['SMTP_DOMAIN'] && config['SMTP_USER_NAME'] && config['SMTP_PASSWOR
105 111
   puts "Okay, let's setup outgoing email settings.  The simplest solution is to use the free sendgrid Heroku addon."
106 112
   puts "If you'd like to use your own server, or your Gmail account, please see .env.example and set"
107 113
   puts "SMTP_DOMAIN, SMTP_USER_NAME, SMTP_PASSWORD, and SMTP_SERVER with 'heroku config:set'."
108
-  if yes?("Should I enable the free sendgrid addon? (y/n)")
114
+  if yes?("Should I enable the free sendgrid addon?")
109 115
     puts capture("heroku addons:add sendgrid")
110 116
     puts capture("heroku config:set SMTP_SERVER=smtp.sendgrid.net")
111 117
     puts capture("heroku config:set SMTP_DOMAIN=heroku.com")
@@ -124,4 +130,29 @@ unless config['SMTP_DOMAIN'] && config['SMTP_USER_NAME'] && config['SMTP_PASSWOR
124 130
   end
125 131
 end
126 132
 
133
+branch = capture("git rev-parse --abbrev-ref HEAD")
134
+if yes?("Should I push your current branch (#{branch}) to heroku?")
135
+  puts "This may take a moment..."
136
+  puts capture("git push heroku #{branch}:master -f")
137
+
138
+  puts "Running database migrations..."
139
+  puts capture("heroku run rake db:migrate")
140
+
141
+  puts
142
+  puts
143
+  puts "I can make an admin user on your new Huginn instance and setup some example Agents."
144
+  if yes?("Should I create a new admin user and some example Agents?")
145
+    seed_email = nag "Okay, what is your email address?"
146
+    seed_username = nag "And what username would you like to login as?"
147
+    seed_password = nag "Finally, what password would you like to use?", noecho: true
148
+    puts "\nJust a moment..."
149
+
150
+    capture("heroku run rake db:seed SEED_EMAIL=#{seed_email} SEED_USERNAME=#{seed_username} SEED_PASSWORD=#{seed_password}")
151
+    puts
152
+    puts
153
+    puts "Okay, you should be all set!  Visit https://#{app_name}.herokuapp.com and login as '#{seed_username}' with your password."
154
+  end
155
+end
156
+
157
+puts
127 158
 puts "Done!"

+ 4 - 4
db/seeds.rb

@@ -1,10 +1,10 @@
1 1
 # This file should contain all the record creation needed to seed the database with its default values.
2 2
 # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
3 3
 
4
-user = User.find_or_initialize_by(:email => "admin@example.com")
5
-user.username = "admin"
6
-user.password = "password"
7
-user.password_confirmation = "password"
4
+user = User.find_or_initialize_by(:email => ENV['SEED_EMAIL'] || "admin@example.com")
5
+user.username = ENV['SEED_USERNAME'] || "admin"
6
+user.password = ENV['SEED_PASSWORD'] || "password"
7
+user.password_confirmation = ENV['SEED_PASSWORD'] || "password"
8 8
 user.invitation_code = User::INVITATION_CODES.first
9 9
 user.admin = true
10 10
 user.save!